package com.xinqi.modules.exam.task.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.exam.task.convert.AsyncTaskProcessLogConvert;
import com.xinqi.modules.exam.task.domain.AsyncTaskProcessLogEntity;
import com.xinqi.modules.exam.task.dto.req.AsyncTaskProcessLogCreateDTO;
import com.xinqi.modules.exam.task.dto.req.AsyncTaskProcessLogDeleteDTO;
import com.xinqi.modules.exam.task.dto.req.AsyncTaskProcessLogQueryDTO;
import com.xinqi.modules.exam.task.dto.req.AsyncTaskProcessLogUpdateDTO;
import com.xinqi.modules.exam.task.dto.rsp.AsyncTaskProcessLogResultDTO;
import com.xinqi.modules.exam.task.manager.AsyncTaskProcessLogManager;
import com.xinqi.modules.exam.task.service.AsyncTaskProcessLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 异步任务处理日志服务层实现
 *
 * @author: linkai
 * @date: 2023/07/08
 */
@Validated
@Service("asyncTaskProcessLogService")
@RequiredArgsConstructor
public class AsyncTaskProcessLogServiceImpl implements AsyncTaskProcessLogService {
    private final AsyncTaskProcessLogConvert asyncTaskProcessLogConvert;
    private final AsyncTaskProcessLogManager asyncTaskProcessLogManager;

    /**
     * 分页查询 异步任务处理日志数据
     */
    @Override
    @DS("slaver")
    public PageInfo<AsyncTaskProcessLogResultDTO> page(Param pageable, AsyncTaskProcessLogQueryDTO query) {
        IPage<AsyncTaskProcessLogEntity> page = asyncTaskProcessLogManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, asyncTaskProcessLogConvert::convert);
    }

    /**
     * 查询 异步任务处理日志数据
     */
    @Override
    public List<AsyncTaskProcessLogResultDTO> find(AsyncTaskProcessLogQueryDTO query) {
        return asyncTaskProcessLogManager.list(wrapper(query)).stream()
            .map(asyncTaskProcessLogConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<AsyncTaskProcessLogEntity> wrapper(AsyncTaskProcessLogQueryDTO query) {
        LambdaQueryWrapper<AsyncTaskProcessLogEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(AsyncTaskProcessLogEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 异步任务处理日志数据
     */
    @Override
    public AsyncTaskProcessLogResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(asyncTaskProcessLogManager.findById(id)).map(asyncTaskProcessLogConvert::convert).orElse(null);
    }

    /**
     * 新增 异步任务处理日志数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid AsyncTaskProcessLogCreateDTO dto) {
        AsyncTaskProcessLogEntity entity = asyncTaskProcessLogConvert.create(dto);
        asyncTaskProcessLogManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 异步任务处理日志数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid AsyncTaskProcessLogUpdateDTO dto) {
        Optional.ofNullable(asyncTaskProcessLogManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        AsyncTaskProcessLogEntity entity = asyncTaskProcessLogConvert.update(dto);
        return asyncTaskProcessLogManager.updateById(entity);
    }

    /**
     * 根据id删除 异步任务处理日志数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return asyncTaskProcessLogManager.removeById(id);
    }

    /**
     * 根据id删除 异步任务处理日志数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(AsyncTaskProcessLogDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
